package com.szy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szy.mapper.ArticleMapper;
import com.szy.pojo.Article;
import com.szy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    //格式化模板
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
    private final int PAGE_SIZE = 6;
    private final int POPULAR_ARTICLE_CNT = 4;
    @Override
    public PageInfo<Article> queryAll(int pageNum) {
        PageHelper.startPage(pageNum,PAGE_SIZE);
        List<Article> articles = articleMapper.queryAll();
        //对创建日期做格式化
        articles.forEach(article ->{
            article.setArticleCreateTimeFormat(sdf.format(article.getArticleCreateTime()));
        });
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public PageInfo<Article> queryAllAndAuthor(int pageNum){
        PageHelper.startPage(pageNum,6);
        List<Article> articles = articleMapper.queryAllAndAuthor();
        //对创建日期做格式化
        articles.forEach(article -> {
            article.setArticleCreateTimeFormat(sdf.format(article.getArticleCreateTime()));
        });
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public void addArticle(Article article) {
        articleMapper.addArticle(article);
    }

    @Override
    public Article queryArticleAndAuthor(int articleId){
        Article article = articleMapper.queryArticleAndAuthor(articleId);
        article.setArticleCreateTimeFormat(sdf.format(article.getArticleCreateTime()));
        return article;
    }

    @Override
    public List<Article> popularArticles() {
        List<Article> articles = articleMapper.queryPopularArticles(POPULAR_ARTICLE_CNT);
        LocalDate now = LocalDate.now();
        articles.forEach(article ->{
            LocalDate createDate = LocalDate.ofInstant(article.getArticleCreateTime().toInstant(), ZoneId.systemDefault());
            Period period = Period.between(createDate, now);
            int years = period.getYears();
            int months = period.getMonths();
            int days = period.getDays();
//            System.out.println("~~~~~~~~~"+years+"~~~~~~"+months+"~~~~~~~~~~~"+days+"~~~~~~~~~");
            if(years>=1){
                article.setArticleCreatePeriod(years+"年前");
            }
            else if(months>=1){
                article.setArticleCreatePeriod(months+"月前");
            }
            else if(days>=8){
                int weeks = days / 7;
                article.setArticleCreatePeriod(weeks+"周前");
            }
            else if(days>=1){
                article.setArticleCreatePeriod(days+"天前");
            }
            else{
                article.setArticleCreatePeriod("今天");
            }
        });
        return articles;
    }

    @Override
    public Article queryNewestArticle() {
        return articleMapper.queryNewestArticle();
    }
}
